package com.cino.org.service.impl;
import com.cino.basic.base.mapper.BaseMapper;
import com.cino.basic.base.service.impl.BaseServiceImpl;
import com.cino.basic.util.Md5Utils;
import com.cino.basic.util.PageList;
import com.cino.basic.util.StrUtils;
import com.cino.org.domain.Department;
import com.cino.org.domain.Employee;
import com.cino.org.mapper.EmployeeMapper;
import com.cino.org.query.EmployeeQuery;
import com.cino.org.service.EmployeeService;
import com.cino.users.domain.Logininfo;
import com.cino.users.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：部门
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {


    @Autowired
    private BaseMapper<Department> baseMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper  employeeMapper;

    @Override
    public List<Department> deptTree() {
        //获取所有的department对象
        HashMap<Long, Department> hashMap = new HashMap<>();
        List<Department> departments = baseMapper.loadAll();
        for (Department department : departments) {
            hashMap.put(department.getId(), department);
        }
        //前端需要得到的集合数据 - 部门树
        List<Department> deptTree = new ArrayList<>();

        for (Department department : departments) {
            if (department.getParent_id() == null) {
                deptTree.add(department);
            } else {//不是顶级部门
                //1.想办法找到当前部门的 父部门
                Long parent_id = department.getParent_id();
                //这种从数据库查询的方式，每次通过id去查询 -
                // 查询出来映射给实体domain，children集合中都会是空集合 - 没有任何数据
                Department parentDepartment = hashMap.get(parent_id);
                parentDepartment.getChildren().add(department);
            }
        }
        return deptTree;
    }



    @Transactional
    @Override
    public void del(Long id) {
        //删除t_logininfo信息
        Employee employee = employeeMapper.loadById(id);
        logininfoMapper.del(employee.getLogininfo_id());

        //删除当前员工的角色信息:t_employee_role
        employeeMapper.delRoleByEmployeeId(id);

        //删除t_employee信息
        super.del(id);
    }

    @Transactional
    @Override
    public void save(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //创建Logininfo对象
        Logininfo logininfo = employee2Logininfo(employee);
        //先保存Logininfo对象 - 生成自增id
        logininfoMapper.save(logininfo);
        //将id设置到employee对象中
        employee.setLogininfo_id(logininfo.getId());
        //添加管理员信息
        super.save(employee);

        //添加当前员工的角色信息
        employeeMapper.addRole(employee);

    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        //通过属性同名原则进行拷贝
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        return logininfo;
    }

    @Transactional
    @Override
    public void update(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //注意:修改的时候不应该修改密码 - 强制修改密码

        //创建Logininfo对象:拷贝了6个数据包括id[employee的id]
        Logininfo logininfo = employee2Logininfo(employee);
        Employee dbEmployee = employeeMapper.loadById(employee.getId());
        //重新设置Logininfoid
        logininfo.setId(dbEmployee.getLogininfo_id());
        Logininfo dbLogininfo = logininfoMapper.loadById(dbEmployee.getLogininfo_id());
        logininfo.setDisable(dbLogininfo.getDisable());

        //操作数据库
        logininfoMapper.update(logininfo);
        //employee - 前端传递的
        employee.setShop_id(dbEmployee.getShop_id());
        employee.setLogininfo_id(dbEmployee.getLogininfo_id());
        super.update(employee);

        //修改 = 先删再添加
        employeeMapper.delRoleByEmployeeId(employee.getId());
        employeeMapper.addRole(employee);
    }

    //批量删除
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public List<Employee> queryByShopId(Long shopId) {
        return employeeMapper.queryByShopId(shopId);
    }

}
